import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { PerLaborIncomeApi } from '#/api/per/laborIncome';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { formatDate } from '@vben/utils';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'year',
      label: '年度',
      component: 'DatePicker',
      componentProps: {
        picker: 'year',
        format: 'YYYY',
        valueFormat: 'YYYY',
      },
    },
    {
      fieldName: 'lectureFee',
      label: '讲学收入',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入讲学收入',
        addonAfter: '元',
      },
    },
    {
      fieldName: 'writingFee',
      label: '写作收入',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入写作收入',
        addonAfter: '元',
      },
    },
    {
      fieldName: 'consultingFee',
      label: '咨询收入',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入咨询收入',
        addonAfter: '元',
      },
    },
    {
      fieldName: 'reviewFee',
      label: '审稿收入',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入审稿收入',
        addonAfter: '元',
      },
    },
    {
      fieldName: 'paintingFee',
      label: '书画收入',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入书画收入',
        addonAfter: '元',
      },
    },
    {
      fieldName: 'otherFee',
      label: '其他收入',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入其他收入',
        addonAfter: '元',
      },
    },
    {
      fieldName: 'totalAmount',
      label: '合计收入',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入合计收入',
        addonAfter: '元',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '人员ID，对应 per_employee.id',
      field: 'employeeId',
    },
    {
      label: '讲学收入',
      field: 'lectureFee',
    },
    {
      label: '写作收入',
      field: 'writingFee',
    },
    {
      label: '咨询收入',
      field: 'consultingFee',
    },
    {
      label: '审稿收入',
      field: 'reviewFee',
    },
    {
      label: '书画收入',
      field: 'paintingFee',
    },
    {
      label: '其他收入',
      field: 'otherFee',
    },
    {
      label: '合计收入',
      field: 'totalAmount',
    },
    {
      label: '年度',
      field: 'year',
      render: (val) => formatDate(val, 'YYYY') as string,
    },
    {
      label: '状态',
      field: 'status',
      render: (val) =>
        h(
          'span',
          getDictOptions(DICT_TYPE.COMMON_STATUS).find(
            (opt) => opt.value === val,
          )?.label || val,
        ),
    },
    {
      label: '创建时间',
      field: 'createTime',
      render: (val) => formatDate(val, 'YYYY-MM-DD HH:mm:ss') as string,
    },
    {
      label: '更新时间',
      field: 'updateTime',
      render: (val) => formatDate(val, 'YYYY-MM-DD HH:mm:ss') as string,
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'year',
      label: '年度',
      component: 'DatePicker',
      componentProps: {
        picker: 'year',
        format: 'YYYY',
        valueFormat: 'YYYY',
        placeholder: '请选择年度',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<PerLaborIncomeApi.LaborIncome>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '主键ID',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'year',
      title: '年度',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'lectureFee',
      title: '讲学收入',
      minWidth: 120,
    },
    {
      field: 'writingFee',
      title: '写作收入',
      minWidth: 120,
    },
    {
      field: 'consultingFee',
      title: '咨询收入',
      minWidth: 120,
    },
    {
      field: 'reviewFee',
      title: '审稿收入',
      minWidth: 120,
    },
    {
      field: 'paintingFee',
      title: '书画收入',
      minWidth: 120,
    },
    {
      field: 'otherFee',
      title: '其他收入',
      minWidth: 120,
    },
    {
      field: 'totalAmount',
      title: '合计收入',
      minWidth: 120,
    },

    {
      field: 'status',
      title: '状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
